package test;

import java.io.File;
import sample.xmlbean.*;

import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;

public class CustomerXMLBean {

    private String filename = null;

    public CustomerXMLBean(String filename) {
        super();
        this.filename = filename;
    }

    public void customerReader() {
        try {
            File xmlFile = new File(filename);
            CustomersDocument doc = CustomersDocument.Factory.parse(xmlFile);
            CustomerType[] customers = doc.getCustomers().getCustomerArray();

            for (int i = 0; i < customers.length; i++) {
                CustomerType customer = customers[i];
                println("Customer#" + i);
                println("Customer ID:" + customer.getId());
                println("First name:" + customer.getFirstname());
                println("Last name:" + customer.getLastname());
                println("Gender:" + customer.getGender());
                println("PhoneNumber:" + customer.getPhoneNumber());
                // Primary address
                PrimaryAddressType primaryAddress = customer.getAddress()
                        .getPrimaryAddress();
                println("PrimaryAddress:");
                println("PostalCode:" + primaryAddress.getPostalCode());
                println("AddressLine1:" + primaryAddress.getAddressLine1());
                println("AddressLine2:" + primaryAddress.getAddressLine2());
                // Billing address
                BillingAddressType billingAddress = customer.getAddress()
                        .getBillingAddress();
                println("BillingAddress:");
                println("Receiver:" + billingAddress.getReceiver());
                println("PostalCode:" + billingAddress.getPostalCode());
                println("AddressLine1:" + billingAddress.getAddressLine1());
                println("AddressLine2:" + billingAddress.getAddressLine2());
            }
            
            XmlCursor resultCursor = doc.newCursor().execQuery("$this/Customers");
            System.out.println(resultCursor.xmlText());
            
            String xmlPathStatement = "$this/Customers/customer/id";
//            CustomerType[] cstms = (CustomerType[])doc.selectPath(xmlPathStatement);
            XmlObject[] rslt = doc.selectPath(xmlPathStatement);
            System.out.println(rslt.length);
//            for (int i = 0; i < cstms.length; i++) {
//                CustomerType customer = customers[i];
//                println("Customer#" + i);
//                println("Customer ID:" + customer.getId());
//                println("First name:" + customer.getFirstname());
//                println("Last name:" + customer.getLastname());
//                println("Gender:" + customer.getGender());
//                println("PhoneNumber:" + customer.getPhoneNumber());
//                // Primary address
//                PrimaryAddressType primaryAddress = customer.getAddress()
//                        .getPrimaryAddress();
//                println("PrimaryAddress:");
//                println("PostalCode:" + primaryAddress.getPostalCode());
//                println("AddressLine1:" + primaryAddress.getAddressLine1());
//                println("AddressLine2:" + primaryAddress.getAddressLine2());
//                // Billing address
//                BillingAddressType billingAddress = customer.getAddress()
//                        .getBillingAddress();
//                println("BillingAddress:");
//                println("Receiver:" + billingAddress.getReceiver());
//                println("PostalCode:" + billingAddress.getPostalCode());
//                println("AddressLine1:" + billingAddress.getAddressLine1());
//                println("AddressLine2:" + billingAddress.getAddressLine2());
//            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void println(String str) {
        System.out.println(str);
    }

    public static void main(String[] args) {
        String filename = "E://customers.xml";
        CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
        customerXMLBean.customerReader();
    }
}
